namespace Gl.SettingsManager.Providers.DbSettings
{
	internal class DbSettingsManager
	{
		private readonly bool _useTransaction;
		private readonly string _applicationName;
		private readonly string _connectionString;

		public DbSettingsManager(bool useTransaction, string applicationName, string connectionString)
		{
			_useTransaction = useTransaction;
			_applicationName = applicationName;
			_connectionString = connectionString;

			if (!SettingsDataBaseExists())
				InitDataBase();
		}

		public SettingsCollection GetSettings(string typeName, string context)
		{
			return DbHelper.GetSettingsCollection(_applicationName, context, typeName, _connectionString);
		}

		public void SaveSettings(SettingsCollection collection)
		{
			DbHelper.SaveSettingsCollection(collection, _applicationName, _connectionString, _useTransaction);
		}


		public void ResetSettings()
		{
			DbHelper.ResetSettings(_connectionString);
		}

		private void InitDataBase()
		{
			DbHelper.CreateTable(_connectionString);
		}

		private bool SettingsDataBaseExists()
		{
			return DbHelper.SettingsDataBaseExists(_connectionString);
		}

	}
}
